package com.yxq.mdt.app.modules.personal.ui.fragment;

import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.modules.personal.adapter.CollectTopicListAdapter;
import com.yxq.mdt.app.modules.personal.bean.CollectTopicListBean;
import com.yxq.mdt.app.modules.personal.bean.PublishBean;
import com.yxq.mdt.app.modules.personal.bean.TopicCommentBean;
import com.yxq.mdt.app.modules.personal.model.PublishModel;
import com.yxq.mdt.app.modules.personal.presenter.IPublishContract;
import com.yxq.mdt.app.modules.personal.presenter.PublishPresenter;
import com.yxq.mdt.app.modules.socialcontact.bean.GroupTopicListBean;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialPersonalHomepageAc;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialStateDetailsAc;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.utils.MediaPlayerHelper;

import butterknife.BindView;
import cn.jzvd.Jzvd;

/**
 * @author L
 * @time 2019-11-19
 * @describe 收藏-帖子
 */
public class CollectArticleFragment extends BaseFragment<PublishPresenter> implements IPublishContract.View {
    public static final String EXTRA_TEXT = "extra_text";
    @BindView(R.id.social_state_recyclerView)
    RecyclerView socialStateRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    CollectTopicListAdapter collectTopicListAdapter;

    @Override
    protected int attachLayoutId() {
        return R.layout.fragment_collect_tab;
    }

    @Override
    protected PublishPresenter getPresenter() {
        return new PublishPresenter(this, new PublishModel());
    }

    @Override
    public void initView(View view) {
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.selectMyTopicFavour(page, 20);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.selectMyTopicFavour(page, 20);
        });
        socialStateRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        collectTopicListAdapter = new CollectTopicListAdapter(R.layout.item_layout_collect_list, null);
        socialStateRecyclerView.setAdapter(collectTopicListAdapter);
        collectTopicListAdapter.setOnItemClickListener((adapter, view1, position) -> {
            Intent it = new Intent(mContext, SocialStateDetailsAc.class);
            it.putExtra("topicId", collectTopicListAdapter.getItem(position).getTopicId());
            it.putExtra("id", collectTopicListAdapter.getItem(position).getTopicId());
            startActivity(it);
        });

        socialStateRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(@NonNull View view) {
            }

            @Override
            public void onChildViewDetachedFromWindow(@NonNull View view) {
                try {
                    Jzvd jzvd = view.findViewById(R.id.item_social_video_player);
                    if (jzvd != null && Jzvd.CURRENT_JZVD != null && jzvd.jzDataSource.containsTheUrl(Jzvd.CURRENT_JZVD.jzDataSource.getCurrentUrl())) {
                        if (Jzvd.CURRENT_JZVD != null && Jzvd.CURRENT_JZVD.screen != Jzvd.SCREEN_FULLSCREEN) {
                            Jzvd.releaseAllVideos();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        socialStateRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (collectTopicListAdapter.countDownTimer != null) {
                    collectTopicListAdapter.countDownTimer.onFinish();
                    MediaPlayerHelper.pause();
                }
            }
        });
        collectTopicListAdapter.setEmptyView(R.layout.layout_empty_view, socialStateRecyclerView);
        collectTopicListAdapter.isUseEmpty(false);
    }

    @Override
    public void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                getActivity().finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onPause() {
        super.onPause();
        if (collectTopicListAdapter.countDownTimer != null) {
            collectTopicListAdapter.countDownTimer.cancel();
        }
        Jzvd.goOnPlayOnPause();
        MediaPlayerHelper.realese();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        super.onPause();
        if (collectTopicListAdapter.countDownTimer != null) {
            collectTopicListAdapter.countDownTimer.cancel();
        }
        Jzvd.goOnPlayOnPause();
        MediaPlayerHelper.realese();
    }

    @Override
    public void onLoadListSuccess(PublishBean data) {

    }

    @Override
    public void onLoadListFailed(String msg) {

    }

    @Override
    public void onLoadToPicListSuccess(CollectTopicListBean data) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        if (page == 1) {
            collectTopicListAdapter.setNewData(data.getList());
        } else {
            if (data.getList() != null && data.getList().size() != 0) {
                collectTopicListAdapter.addData(data.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        collectTopicListAdapter.isUseEmpty(!(collectTopicListAdapter.getData().size() > 0));
        collectTopicListAdapter.setOnItemChildClickListener((adapter, view12, position) -> {
            switch (view12.getId()) {
                case R.id.item_layout_nike_img:
                    //跳转个人中心主页
                    Intent intent = new Intent(mContext, SocialPersonalHomepageAc.class);
                    intent.putExtra("userId", collectTopicListAdapter.getItem(position).getUserId());
                    startActivity(intent);
                    break;
                case R.id.item_layout_attention:
                    //关注
                    TextView attention = view12.findViewById(R.id.item_layout_attention);
                    MainSocialContactModel.setInterestRelation(collectTopicListAdapter.getItem(position).getUserId() + "", 1, 1, new ApiModel.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String s) {
                            attention.setVisibility(View.GONE);
                            showCenterToast("关注成功");
                        }

                        @Override
                        public void onError(String errMsg) {
                            showCenterToast("关注失败");
                        }
                    });
                    break;
                case R.id.item_layout_content_browse_img_lint:
                    //点赞
                    int giveNum = collectTopicListAdapter.getItem(position).getUpCount();
                    ImageView imageView = view12.findViewById(R.id.item_layout_content_browse_img);
                    if (collectTopicListAdapter.getItem(position).getGiveLike() != 0) {
                        MainSocialContactModel.delGsTopicFavour("2", collectTopicListAdapter.getItem(position).getTopicId(), new ApiModel.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String s) {
                                imageView.setImageResource(R.mipmap.icon_community_fabulous);
                                collectTopicListAdapter.getItem(position).setGiveLike(0);
                                collectTopicListAdapter.getItem(position).setUpCount(giveNum - 1);
                                showCenterToast("取消点赞成功");
                            }

                            @Override
                            public void onError(String errMsg) {
                                showCenterToast("取消点赞失败");
                            }
                        });
                    } else {
                        MainSocialContactModel.saveGsTopicFavour(collectTopicListAdapter.getItem(position).getTopicId(), "2", new ApiModel.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String s) {
                                imageView.setImageResource(R.mipmap.zan);
                                collectTopicListAdapter.getItem(position).setGiveLike(1);
                                collectTopicListAdapter.getItem(position).setUpCount(giveNum + 1);
                                collectTopicListAdapter.notifyDataSetChanged();
                                showCenterToast("点赞成功");
                            }

                            @Override
                            public void onError(String errMsg) {
                                showCenterToast("点赞失败");
                            }
                        });
                    }
                    break;
                default:


            }
        });
    }

    @Override
    public void onLoadToPicListFailed(String msg) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onLoadTopicCommentListSuccess(TopicCommentBean data) {

    }

    @Override
    public void onLoadTopicCommentFailed(String msg) {

    }

}